package webx.actions;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

public class OpenFileRunnable extends Test implements Runnable {
	
	String pagepath = ""; 
	
	int row = -1;
	
	public OpenFileRunnable() {
	}
	
	public OpenFileRunnable(String pagepath,String row ) {
		this.pagepath = pagepath;
		if(row!=null && !"".equals(row)){
			try{
				this.row = Integer.valueOf(row);
			}catch(Throwable e){}
		}
	}
	public static void main(String[]args){
		System.out.println(null instanceof Test );
	}
	public void run() {
		try
		{   
			IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
			
//			IEditorPart editor = page.getActiveEditor();
//			// System.out.println("getActiveEditor:"+editor+"; "+(editor instanceof sssex.editors.MultiPageEditor));
//			if(editor instanceof sssex.editors.MultiPageEditor){
//				// com.genuitec.eclipse.webdesigner3.editors.WebDesignerMultiPageEditor pageEdtior
//				((sssex.editors.MultiPageEditor)editor).getEditor().getSourceEditor().getTextViewer().doOperation(org.eclipse.wst.sse.ui.internal.StructuredTextViewer.UNDO);
//				IDocument doc = ((sssex.editors.MultiPageEditor)editor).doc;
//				System.out.println("docq:"+doc.getClass());
//				if (doc instanceof IStructuredDocument){
//					System.out.println("doc:");
//				}
//			}
			//IEditorPart[] editors = page.getEditors();
			//for(int i=0;i<editors.length;i++){
				//System.out.println(editors[i].getSite()+";"+editors[i].getTitle());
			//}
			//page.getActiveEditor().getEditorSite().
			//获得文本编辑对象的document
			IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(pagepath));
			// System.out.println("file"+file);
			if(this.row!=-1){
				IMarker marker = file.createMarker(IMarker.BOOKMARK);
				//marker.setAttribute(IMarker. );
				//marker.setAttribute(IMarker.LOCATION, true);
				marker.setAttribute(IMarker.MESSAGE, "marker");
				marker.setAttribute(IMarker.LINE_NUMBER, this.row);
				//marker.setAttribute(IMarker.TRANSIENT, true);
				//marker.setAttribute(IMarker.LOCATION, 70);
				IDE.openEditor(page, marker);
				//IDE
				marker.delete();
			}else{
				IDE.openEditor(page,file);
			}
		}catch (Throwable e){  
			e.printStackTrace();
		}
	}
	
}
